package com.gjun.struts.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.gjun.struts.bean.GjunMainTypeJsonBean;
import com.gjun.struts.bean.GjunNews;
import com.gjun.struts.bean.GjunNewsJsonBean;
import com.gjun.struts.bean.GjunsystemUserJsonBean;
import com.gjun.struts.resource.GjunMainTypeResourceIf;
import com.gjun.struts.resource.GjunNewsResourceIf;
import com.gjun.struts.resource.GjunShopDAOIf;
import com.gjun.struts.resource.GjunSystemUserResourceIf;
import com.gjun.struts.util.JSONHelper;

public class MgrSystemNewsAction {
    private String result;   // 向客户端返回当前查询结果得到的json集合对象
    
    
    private String maintype_json;  // 返回新闻主类型的下拉框 json 集合对象
    
    private String users_json; // 返回系统用户的下拉框 json 集合对象
    
    private int pageSize = 10;  //<param name="includeProperties">result,maintype_json,users_json,totalSize,totalPage,curPage</param>
    
    private int totalSize;
    
    private int totalPage;
    
    private int curPage;
    
    // 接收客户端的查询条件
    private String maintype;
    
    private String state;
    
    private String users;
    
    private Date starttimer;
    
    private Date endtimer;
    
    private int id;
    
    private String ntitle;
    
    private GjunMainTypeResourceIf maintypeResource;
    private GjunSystemUserResourceIf userResource;
    private GjunNewsResourceIf newResource;
    
    // 处理删除
    private int[] delId;   // delId=2&delId=3&
    
    // 处理更新
    //title, newkey, opennews, endnews, maintypeid, infoContent
    private String title;
    private String newkey;
    private Date opendate;
    private Date enddate;
    private String infoContent;
    
    
    
    
	
	public Date getEnddate() {
		return enddate;
	}
	public void setEnddate(Date enddate) {
		this.enddate = enddate;
	}
	public String getInfoContent() {
		return infoContent;
	}
	public void setInfoContent(String infoContent) {
		this.infoContent = infoContent;
	}
	public String getNewkey() {
		return newkey;
	}
	public void setNewkey(String newkey) {
		this.newkey = newkey;
	}
	public Date getOpendate() {
		return opendate;
	}
	public void setOpendate(Date opendate) {
		this.opendate = opendate;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public int[] getDelId() {
		return delId;
	}
	public void setDelId(int[] delId) {
		this.delId = delId;
	}
	public String getNtitle() {
		return ntitle;
	}
	public void setNtitle(String ntitle) {
		this.ntitle = ntitle;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public GjunNewsResourceIf getNewResource() {
		return newResource;
	}
	public void setNewResource(GjunNewsResourceIf newResource) {
		this.newResource = newResource;
	}
	public Date getEndtimer() {
		return endtimer;
	}
	public void setEndtimer(Date endtimer) {
		this.endtimer = endtimer;
	}
	public String getMaintype() {
		return maintype;
	}
	public void setMaintype(String maintype) {
		this.maintype = maintype;
	}
	public Date getStarttimer() {
		return starttimer;
	}
	public void setStarttimer(Date starttimer) {
		this.starttimer = starttimer;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getUsers() {
		return users;
	}
	public void setUsers(String users) {
		this.users = users;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getTotalSize() {
		return totalSize;
	}
	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}
	public String getMaintype_json() {
		return maintype_json;
	}
	public void setMaintype_json(String maintype_json) {
		this.maintype_json = maintype_json;
	}
	public String getUsers_json() {
		return users_json;
	}
	public void setUsers_json(String users_json) {
		this.users_json = users_json;
	}
	public GjunMainTypeResourceIf getMaintypeResource() {
		return maintypeResource;
	}
	public void setMaintypeResource(GjunMainTypeResourceIf maintypeResource) {
		this.maintypeResource = maintypeResource;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public GjunSystemUserResourceIf getUserResource() {
		return userResource;
	}
	public void setUserResource(GjunSystemUserResourceIf userResource) {
		this.userResource = userResource;
	}
    
    public String initSelectOptionData(){
//    	List<GjunMainTypeJsonBean> beans = maintypeResource.loadAllMainType();
//    	List<GjunsystemUserJsonBean> users = userResource.loadAllUsersToJsonBean();
//    	try {
//			maintype_json = JSONHelper.mainTypesToJson(beans);   // eval("var xxx="+data.maintype_json)
//			users_json = JSONHelper.usersToJson(users);
//			
//		} catch (Throwable e) {
//		    e.printStackTrace();
//		}
//    	System.out.println(maintype_json);
//    	System.out.println(users_json);
    	return "success";
    }
    
    public String loadNewByCondition(){
    	
    	totalSize = newResource.loadNewsCountByConditions(maintype, state, users, starttimer, endtimer);
    	
    	totalPage = (totalSize + pageSize - 1) / pageSize;
    	
    	if(curPage==0) curPage = 1;
    	
    	int start = (curPage - 1) * pageSize;
    	
    	List<GjunNewsJsonBean> news = newResource.loadNewsByConditions(maintype, state, users, starttimer, endtimer, start, pageSize);
    	
    	try {
			result = JSONHelper.newsToJson(news);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println("c:  "+curPage);
    	return "success";
    	
    	// totalSize + totalPage + result
    }
    
    
    public String loadNewsById(){ 
    	GjunNewsJsonBean bean = newResource.loadNewsById(id);
    	ServletActionContext.getRequest().getSession().setAttribute("show_news_"+id, bean);
    	//System.out.println("当前bean的状态:  "+bean.getState());
    	//System.out.println("到了action");
    	result = "loadSuccess";
    	return "success";
    }
    
    
    public String addNewsToActiceQueue(){
    	//System.out.println("到了ac===================================xxx w  "+ntitle);
    	HttpSession session = ServletActionContext.getRequest().getSession();
    	TreeMap<Integer,String> news = (TreeMap<Integer, String>)session.getAttribute("want_active_news_queue");
    	if(news==null){
    		news = new TreeMap<Integer,String> ();
    		session.setAttribute("want_active_news_queue",news);
    	}
    	
    	if(news.containsKey(id)){
    		result = "newsExist";
    	}else{
    		news.put(id, ((GjunNewsJsonBean)session.getAttribute("show_news_"+id)).getTitle());
    		result = "newsSaveSuccess";
    	}
    	//System.out.println("news:  "+news);
    	return "success";
    }
    
    
    public String loadActiveQueue(){
    	HttpSession session = ServletActionContext.getRequest().getSession();
    	TreeMap<Integer,String> news = (TreeMap<Integer, String>)session.getAttribute("want_active_news_queue");
    	if(news==null || news.size()==0){
    		result = "noActiveQueueData";
    	}else{
    		try {
				Iterator<Map.Entry<Integer,String>> it = news.entrySet().iterator();
				List<GjunNewsJsonBean> beans = new ArrayList<GjunNewsJsonBean>();
				while(it.hasNext()){
					Map.Entry<Integer,String> me = it.next();
					beans.add(new GjunNewsJsonBean(me.getKey(),me.getValue()));
				}
				//System.out.println(beans);
				result = JSONHelper.newsToJson(beans);  // data.result
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	//System.out.println("r: "+result);
    	return "success";
    }
    
    public String cancelNewActiveById(){
    	
    	//System.out.println("取消请求到了action");
    	HttpSession session = ServletActionContext.getRequest().getSession();
    	TreeMap<Integer,String> news = (TreeMap<Integer, String>)session.getAttribute("want_active_news_queue");
    	news.remove(id);
    	if(!news.containsKey(id))
    	result = "cancelSuccess";
    	else
    	result = "cancelFaild";
    	return "success";
    }
    
    
    public String clearActiveListNews(){
    	HttpSession session = ServletActionContext.getRequest().getSession();
    	session.removeAttribute("want_active_news_queue");
    	result = "clearSuccess";
    	return "success";
    }
    
    public String batchUpdateNewsQueueState(){
    	HttpSession session = ServletActionContext.getRequest().getSession();
    	TreeMap<Integer,String> news = (TreeMap<Integer, String>)session.getAttribute("want_active_news_queue");
        int[] ids = new int[news.size()];
        
        //Integer[] ids  = (Integer[])news.keySet().toArray();
        Iterator<Integer> set = news.keySet().iterator();
        int count = 0;
        while(set.hasNext()){
        	ids[count++] = set.next().intValue();
        }
    	try {
			newResource.updateNewsQueueState(ids);
			result = "batchUpdateSuccess";
			session.removeAttribute("want_active_news_queue");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "batchUpdateFaild";
		}
		
		return "success";
    }
    
    
    public String updateNewsStateToCancelOrReset(){
    	try {
			newResource.updateNewsStateToCancelOrReset(id,Integer.parseInt(state));
			result = "serviceSuccess";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "serviceSuccess";
		}
		
		return "success";
    }
    
    
    public String delNewById(){
    	try {
			newResource.delNewById(id);
			result = "delNewsSuccess";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "delNewsFaild";
		}
        return "success";
    }
    
    
    public String batchDelNews(){
    	//System.out.println(Arrays.toString(delId));
    	try {
			newResource.delNewsUseBatch(delId);
			result = "delBatchSuccess";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "delBatchFaild";
		}
    	return "success";
    }
    
    
    public String loadNewsEditById(){
    	GjunNewsJsonBean bean = newResource.loadNewsEditById(id);
    	ServletActionContext.getRequest().getSession().setAttribute("editNewBean",bean);
    	result = "edit_to_next_page";
    	return "success";
    }
    
    
    /*
    private String title;
    private String newkey;
    private Date opendate;
    private Date enddate;
    private String infoContent;
     * */
    public String updateNewsContent(){
    	try {
			newResource.updateNewContentById(id, title, newkey, opendate, enddate, Integer.parseInt(maintype), infoContent);
			result = "updateNewsInfoSuccess";
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			result = "updateNewsInfoFaild";
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = "updateNewsInfoFaild";
		}
    	return "success";
    }
}





















